<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="消息ID" prop="mid">
        <el-input
          v-model="queryParams.mid"
          placeholder="请输入消息ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送者" prop="from">
        <el-input
          v-model="queryParams.from"
          placeholder="请输入发送者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消息类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择消息类型" clearable>
          <el-option label="私聊" value="1" />
          <el-option label="群聊" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="目标" prop="target">
        <el-input
          v-model="queryParams.target"
          placeholder="请输入目标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内容类型" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择内容类型" clearable>
          <el-option label="文本" value="1" />
          <el-option label="图片" value="2" />
          <el-option label="语音" value="3" />
          <el-option label="视频" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间范围">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sensitiveword:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hitList">
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="消息ID" align="center" prop="mid" width="120" />
      <el-table-column label="发送者" align="center" prop="from" width="120" />
      <el-table-column label="消息类型" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">私聊</span>
          <span v-else-if="scope.row.type == 2">群聊</span>
          <span v-else>{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="目标" align="center" prop="target" width="120" />
      <el-table-column label="行号" align="center" prop="line" width="80" />
      <el-table-column label="消息内容" align="center" prop="data" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="搜索关键词" align="center" prop="searchableKey" width="150" />
      <el-table-column label="内容类型" align="center" prop="contentType" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.contentType == 1">文本</span>
          <span v-else-if="scope.row.contentType == 2">图片</span>
          <span v-else-if="scope.row.contentType == 3">语音</span>
          <span v-else-if="scope.row.contentType == 4">视频</span>
          <span v-else>{{ scope.row.contentType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="命中时间" align="center" prop="dt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listSensitivewordHit } from "@/api/zl/sensitiveword"

export default {
  name: "SensitivewordHitList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 敏感词命中列表数据
      hitList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mid: null,
        from: null,
        type: null,
        target: null,
        contentType: null,
        keyword: null,
        startTime: null,
        endTime: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询敏感词命中列表 */
    getList() {
      this.loading = true
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryParams.startTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      } else {
        this.queryParams.startTime = null
        this.queryParams.endTime = null
      }
      listSensitivewordHit(this.queryParams).then(response => {
        this.hitList = response.rows
        this.total = parseInt(response.total) || 0
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sensitiveword/hit/export', {
        ...this.queryParams
      }, `敏感词命中列表_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>